package osx.mantianx.read;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

/**
 * @author starrysky
 * @title: PoiReadV03
 * @projectName Excel_Poi_EasyExcel
 * @description: 读取不同的数据类型, cell.getCellType()视频中的过时了变成枚举getCellTypeEnum()
 * @date 2021/2/411:01
 */
public class PoiReadV03 {
    public static void main(String[] args) {
        FileInputStream stream = null;
        XSSFWorkbook workbook = null;
        try {
            stream = new FileInputStream("XlsBigTablesV07First.xlsx");
            if (stream != null) {
                workbook = new XSSFWorkbook(stream);

                Sheet sheetAt = workbook.getSheetAt(0);
                //从sheet中获取行数
                for (int rowNumber = 0; rowNumber < sheetAt.getPhysicalNumberOfRows(); rowNumber++) {
                    StringBuilder stringBuilder = new StringBuilder();
                    Row row = sheetAt.getRow(rowNumber);

                    //某一行中获取列数
                    for (int colNumber = 0; colNumber < row.getPhysicalNumberOfCells(); colNumber++) {
                        Cell cell = row.getCell(colNumber);
                        if (cell != null) {
                            CellType cellType = cell.getCellTypeEnum();

                            switch (cellType) {
                                /**
                                 * 字符串
                                 */
                                case STRING:
                                    stringBuilder.append(" | ");
                                    stringBuilder.append(cell.getStringCellValue());
                                    stringBuilder.append(" | ");
                                    break;
                                /**
                                 * 布尔
                                 */
                                case BOOLEAN:
                                    stringBuilder.append(" | ");
                                    stringBuilder.append(cell.getBooleanCellValue());
                                    stringBuilder.append(" | ");
                                    break;
                                /**
                                 * 空
                                 */
                                case BLANK:
                                    break;
                                /**
                                 * 数字，分时间和普通数字
                                 */
                                case NUMERIC:
                                    /**
                                     * 日期
                                     */
                                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                        Date date = cell.getDateCellValue();
                                        stringBuilder.append(" | ");
                                        stringBuilder.append(new DateTime(date).toString("yyyy-MM-dd"));
                                        stringBuilder.append(" | ");
                                    } else {
                                        /**
                                         * 不是日期格式，防止数字过长！
                                         */
                                        cell.setCellType(CellType.STRING);
                                        stringBuilder.append(" | ");
                                        stringBuilder.append(cell);
                                        stringBuilder.append(" | ");
                                    }
                                    break;
                                /**
                                 * 类型错误
                                 */
                                case ERROR:
                                    break;
                                default:
                                    break;
                            }

                        }
                    }
                    System.out.println(stringBuilder);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                    if (workbook != null) {
                        workbook.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
